package com.kmt.plaza.controller;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConfig;
import com.kmt.plaza.config.MyWXpay;
import com.kmt.plaza.entity.Orders;
import com.kmt.plaza.entity.ProductParams;
import com.kmt.plaza.entity.User;
import com.kmt.plaza.sevice.OrderService;
import com.kmt.plaza.vo.ResStatus;
import com.kmt.plaza.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/order")
@Api(value = "订单相关的接口",tags = "订单管理")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @PostMapping("/addOrder")
    @ApiOperation("添加订单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String",name = "cids",value = "购物车记录列表",required =true),
            @ApiImplicitParam(dataType = "Orders",name = "order",value = "订单",required =true),
    })
    public ResultVO addOrder(String cids, @RequestBody Orders order) {
        ResultVO resultVO = null;
        try {
            HashMap<String, String> orderInfo = orderService.addOrder(cids, order);
            String orderId = orderInfo.get("orderId");
//            String orderId = "123456789";
            if(orderId!=null){
                //申请支付连接
                WXPay wxPay = new WXPay(new MyWXpay());
                HashMap<String,String> data = new HashMap<>();
                //商品描述
                data.put("body",orderInfo.get("productNames"));
                //使用当前订单的编号作为当前支付交易的交易号。
                data.put("out_trade_no",orderId);
                //支付币种
                data.put("fee_type","CNY");
                //支付金额
//                data.put("total_fee",order.getActualAmount()*100+"");
                data.put("total_fee","1");
                //交易类型
                data.put("trade_type","NATIVE");
                data.put("notify_url","http://ww.free.idcfengye.com/pay/success");
                System.out.println(data);
//                152016397551
                Map<String, String> resp = wxPay.unifiedOrder(data);
                System.out.println(resp);
                orderInfo.put("payUrl",resp.get("code_url"));
                resultVO = new ResultVO(ResStatus.OK, "订单提交成功！", orderInfo);
            }else{
                resultVO = new ResultVO(ResStatus.NO,"订单提交失败！",null);
            }
        } catch (SQLException e) {
            //是否打印
            e.printStackTrace();
            resultVO = new ResultVO(ResStatus.NO,"订单提交失败！",null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultVO;
    }
    @GetMapping("/status/{orderId}")
    @ApiOperation("通过订单id查询订单状态接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String",name = "orderId",value = "订单id",required =true),
    })
    public ResultVO getOrderStatus(@PathVariable("orderId")String orderId, @RequestHeader("token")String token){
        ResultVO resultVO = orderService.getOrderById(orderId);
        return resultVO;
    }
    @GetMapping("/orderList")
    @ApiOperation("订单查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String",name = "userId",value = "用户id",required =true),
            @ApiImplicitParam(dataType = "String",name = "status",value = "订单状态",required =false),
            @ApiImplicitParam(dataType = "int",name = "pageNum",value = "页码",required =true),
            @ApiImplicitParam(dataType = "int",name = "limit",value = "每页条数",required =true),
    })
    public ResultVO orderList(@RequestHeader("token")String token,String userId,
                              String status,int pageNum,int limit){
        ResultVO resultVO = orderService.OrderList(userId,status,pageNum,limit);
        return resultVO;
    }

    @GetMapping("/selectAllOrderList")
    @ApiOperation("管理员查询所有订单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int",name = "pageNum",value = "页码",required =true),
            @ApiImplicitParam(dataType = "int",name = "limit",value = "每页条数",required =true),
    })
    public ResultVO selectAllOrderList(int pageNum,int limit){
        ResultVO resultVO = orderService.selectAllOrderList(pageNum,limit);
        return resultVO;
    }
    @GetMapping("/selectByStatus")
    @ApiOperation("管理员按订单状态查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int",name = "pageNum",value = "页码",required =true),
            @ApiImplicitParam(dataType = "int",name = "limit",value = "每页条数",required =true),
    })
    public ResultVO selectByStatus(int pageNum,int limit){
        ResultVO resultVO = orderService.selectByStatus(pageNum,limit);
        return resultVO;
    }
    @DeleteMapping("/deleteByOrderId/{orderId}")
    @ApiOperation("删除已关闭订单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String",name = "orderId",value = "订单Id",required =true),
    })
    public ResultVO deleteByOrderId(@PathVariable("orderId") String orderId){
        ResultVO resultVO = orderService.deleteByOrderId(orderId);
        return resultVO;
    }
    @GetMapping("/selectByoOrderCondition")
    @ApiOperation("管理员按条件查询订单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String",name = "keyWord",value = "订单编号",required =false),
            @ApiImplicitParam(dataType = "String",name = "status",value = "订单状态",required =false),
            @ApiImplicitParam(dataType = "String",name = "receiverName",value = "收货人",required =false),
    })
    public ResultVO selectByCondition(String keyWord, String status, Integer payType, String receiverName){
        ResultVO resultVO = orderService.selectByOrderCondition(keyWord,status,payType,receiverName);
        return resultVO;
    }

    @DeleteMapping("/delByOrder/{orderId}")
    @ApiOperation("管理员删除订单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String",name = "orderId",value = "订单id",required =true),
    })
    public ResultVO deleteByoOrder(@PathVariable("orderId") String orderId){
        ResultVO resultVO = orderService.deleteByOrderId(orderId);
        return resultVO;
    }

    @ApiOperation(value = "订单状态为待收货状态")
    @PutMapping(value = "/ToPayUpdateOrderStutas")
    public ResultVO ToPayUpdateOrderStutas(String orderId)
    {
        int i = orderService.TopayUpdateOrderStatus(orderId);
        if(i>0){
            return new ResultVO(ResStatus.OK,"success",i);
        }else {
            return new ResultVO(ResStatus.NO,"fial",null);
        }
    }
    @ApiOperation(value = "商家发货")
    @PutMapping(value = "/immediateDelivery")
    public ResultVO immediateDelivery(String orderId)
    {
        int i = orderService.immediateDelivery(orderId);
        if(i>0){
            return new ResultVO(ResStatus.OK,"success",i);
        }else {
            return new ResultVO(ResStatus.NO,"fial",null);
        }
    }

    @ApiOperation(value = "买家确认收货")
    @PutMapping(value = "/ConfirmReceipt")
    public ResultVO ConfirmReceipt(String orderId)
    {
        int i = orderService.ConfirmReceipt(orderId);
        if(i>0){
            return new ResultVO(ResStatus.OK,"success",i);
        }else {
            return new ResultVO(ResStatus.NO,"fial",null);
        }
    }

}
